package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrdersettingDao;
import com.itheima.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = OrdersettingService.class)
@Transactional
public class OrdersettingServiceImpl implements OrdersettingService {

    @Autowired
    private OrdersettingDao ordersettingDao;


    @Override
    public List<Map> getOrdersettingByMonth(String date) {
        String start = date + "-1";
        String end = date + "-31";

        List<OrderSetting> orderSettingList = ordersettingDao.getOrdersettingByMonth(start, end);

        List<Map> data = new ArrayList<>();

        for (OrderSetting orderSetting : orderSettingList) {
            HashMap map = new HashMap();
            map.put("date", orderSetting.getOrderDate().getDate());
            map.put("number", orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations());
            data.add(map);
        }
        return data;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {

        long count = ordersettingDao.findCountByOderData(orderSetting.getOrderDate());

        if (count > 0) {
            ordersettingDao.updateNumberByOrderData(orderSetting);
        } else {
            ordersettingDao.add(orderSetting);
        }


    }


    @Override
    public void add(List<OrderSetting> orderSettingList) {

        if (orderSettingList != null && orderSettingList.size() > 0) {

            for (OrderSetting orderSetting : orderSettingList) {

                long count = ordersettingDao.findCountByOderData(orderSetting.getOrderDate());

                if (count > 0) {

                    ordersettingDao.updateNumberByOrderData(orderSetting);

                } else {

                    ordersettingDao.add(orderSetting);
                }
            }


        }


    }
}
